﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lehnet.BusinessLayer;
using Lehnet.Models;

namespace Lehnet.WebUI.Controllers
{
    public class MusteriAdresiController : Controller
    {
        //
        // GET: /MusteriAdresleri/

        public PartialViewResult Index(int id)
        {
            var mab = new MusteriAdresiBusiness();
            ViewBag.MusteriId = id;
            return PartialView(mab.GetMusteriAdresleriByMusteriId(id));
        }

        public PartialViewResult Create(int? id, int musteriId)
        {
            var ib = new IlBusiness();
            ViewBag.SehirList = ib.LoadAll();

            var ma = new MusteriAdres();
            ma.MusteriId = musteriId;
            if(id != null)
            {
                var mab = new MusteriAdresiBusiness();
                ma = mab.LoadMusteriAdresiById(id.Value);
            }
            return PartialView(ma);
        }

        [HttpPost]
        public PartialViewResult Create(Models.MusteriAdres musteriAdres)
        {
            var ib = new IlBusiness();
            ViewBag.SehirList = ib.LoadAll();

            var mab = new MusteriAdresiBusiness();
            mab.SaveOrUpdate(musteriAdres);
            ViewBag.Message = "Müşteri adresi başarılı bir şekilde kaydedilmiştir.";
            return PartialView(musteriAdres);
        }

        public JsonResult GetIlcelerAjax(int ilId)
        {
            var ib = new IlceBusiness();
            List<Models.Ilce> ilcelist = ib.GetIlcelerByIlId(ilId);

            JsonResult result = Json(ilcelist);
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }
    }
}
